<template>
    <div class='funeral_item_select' :style="styles2">
       <div class='funeral_item_select' :style="styles2">
            <div style="margin-top: 5px;">
                <el-input placeholder="请输入内容" @keyup.enter.native="goSearch" v-model="names" class="input-with-select" style='width:95%'>
                <el-select v-model="pId" slot="prepend" placeholder="请选择" style='width:120px;'>
                    <el-option label="请选择..." value="" ></el-option>
                    <el-option :label="item.sf_ex2" :value="item.sf_id" v-for="(item,index) in options" :key="index"></el-option>
                </el-select>
                    <el-button slot="append" icon="el-icon-search" @click="goSearch"></el-button>
                </el-input>
            </div>
        <el-table
                :data="tableData"
                stripe
                style="width:100%;"
                width="100%"
                @row-dblclick="funeralItemSelect"
                >
               
                <el-table-column
                prop="whc_ex1"
                label="项目名称"
                >
                
                
                </el-table-column>
                <el-table-column
                prop="sfe_ex1"
                label="成本"
                >
                </el-table-column>
            
                <el-table-column
                prop="whc_ex2"
                label="单位" >
                </el-table-column>
                 <el-table-column
                prop="kcnum"
                label="库存" >
                </el-table-column>
            </el-table>
            <div v-if="showDepartMsg=='Y'" style="font-weight:bold;font-size:26px;color:red">请先选择所属馆</div>
        </div>
    </div>
</template>
<script>
export default {
    name:'warehouse_item_select',
    props:['type','styles','appid','data','extype','expost','exkey'],
    components: {
    },
    data(){
        return{
            tableData:[],
            styles2:this.styles,
            options:[],
            pId:'',
            names:'',
            warehouseId:"",
            wh_ex17:"",
            isDepart:"N",
            showDepartMsg:"N",
            url:'byg/funeral/getFuneralSfItem',
            sessionKey:"",
            isWarehouseChild:""
        }
    },
    created(){
        
       
        if(this.expost){
            this.url=this.expost;
        }
        try{
            if(this.exkey&&this.exkey.isDepart){
                this.isDepart=this.exkey.isDepart;
            }
        }catch(e){}
        try{
            if(this.exkey&&this.exkey.isWarehouseChild){
                this.isWarehouseChild=this.exkey.isWarehouseChild;
            }
        }catch(e){}
        try{
            if(this.exkey&&this.exkey.sessionKey){
                this.sessionKey=this.exkey.sessionKey;
            }
        }catch(e){}

        let sef=this;
        if(this.isDepart!='Y'){
            this.showDepartMsg='N'
            this.$post(this.appid,this.url,{type:"warehouse",type3:this.extype,warehouseId:this.warehouseId,isDepart:this.isDepart,isWarehouseChild:this.isWarehouseChild}).then(data=>{
                sef.tableData=[];
                for(let i=0;i<data.item.length;i++){
                    sef.tableData.push(data.item[i])
                }
                this.options=data.options;
                if(this.sessionKey){
                    sessionStorage.setItem(this.sessionKey,JSON.stringify(sef.tableData))
                    sessionStorage.setItem(this.sessionKey+"load","Y")
                }
                console.log("tableData==========================================");
                console.log(sef.tableData)
            })   
        }else{
            this.options=[];
            sef.tableData=[];
            this.showDepartMsg='Y'
        }
       
       
        console.log("styles2===="+this.styles2+"=="+this.styles)
        console.log(this.data)
    },
    computed:{
        scane:function(){
            console.log("1111111111111111111111111")
            let scane="";
           // try{
                scane=this.data["rkInfo"]["isScan"]
            //}catch(e){}
            return scane;
        }
    },
    watch:{
        data:{
            handler: function (newVal) {
               let wh_ex10=""; 
               if(this.data["rkInfo"].wh_ex10){
                   wh_ex10=this.data["rkInfo"].wh_ex10;
               }
               try{
                if(this.data["rkInfo"]["wh_ex17"]){
                    if(this.wh_ex17!=this.data["rkInfo"]["wh_ex17"]){
                        this.wh_ex17=this.data["rkInfo"]["wh_ex17"];
                        this.goSearch();
                    }
                }
               }catch(e){}
               if(this.warehouseId!=wh_ex10){
                   this.warehouseId=wh_ex10;
                   this.goSearch();
               }
                
            },      
            deep: true    //深度监听
        },
        pId(val){
            this.goSearch()
        }
    },
    methods:{
        funeralItemSelect(row, column, event){
           // console.log(row);
           // this.$emit('funeralItemSelectEvent',JSON.stringify(row));
            console.log("funeralItemSelect=============================")
           console.log(this.data)
           let isOk=true;
           let items=this.data["item"];
           for(let i=0;i<items.length;i++){
               var  item_field=items[i].item_field;
                if(!item_field){
                    item_field="whc_wpid";
                }
               if(items[i][item_field]==row[item_field])
                isOk=false;
           }
           if(isOk){
               let rows=JSON.parse(JSON.stringify(row));
               let item_empty=this.data["item_empty"];
               for(let key in item_empty){
                   let dev=item_empty[key];
                   let val="";
                   if(rows[key]){
                       val=rows[key]
                   }
                   if(val==""&&dev!=""){
                       val=dev;
                       rows[key]=val;
                   }
               }
               console.log(rows);
               try{
                if(this.exkey&&this.exkey.sets){
                    for(let i=0;i<this.exkey.sets.length;i++){
                        for(let keys in this.exkey.sets[i]){
                            rows[keys]=rows[this.exkey.sets[i][keys]]
                        }
                    }
                }
               }catch(e){}
                this.$set(this.data["item"],this.data["item"].length,rows)
           }
          
        },
        addItemByName(name){
            let row=null;
            for(let i=0;i<this.tableData.length;i++){
                let szi_ex1=this.tableData[i].szi_ex1;
                if(szi_ex1==name)
                    row=this.tableData[i];
            }
            if(row!=null)
                this.$emit('funeralItemSelectEvent',JSON.stringify(row));
        },
        goSearch(){
            let sef=this;
            if(this.isDepart=='Y'&&this.wh_ex17==''){
                this.showDepartMsg='Y'
                this.options=[];
                this.tableData=[];
            }else{
                this.showDepartMsg='N'
                this.$post(this.appid,this.url,{type:"warehouse",pId:this.pId,name:this.names,type3:this.extype,warehouseId:this.warehouseId,depart:this.wh_ex17,isDepart:this.isDepart,"isWarehouseChild":this.isWarehouseChild}).then(data=>{
               
                    sef.tableData=[];
                    for(let i=0;i<data.item.length;i++){
                        sef.tableData.push(data.item[i])
                    }
                        this.options=data.options;
                    if(this.sessionKey){
                        sessionStorage.setItem(this.sessionKey,JSON.stringify(sef.tableData))
                        sessionStorage.setItem(this.sessionKey+"load","Y")
                    }
                })
            }
            
        }
    }
}
</script>
<style scoped>
.funeral_item_select{
    width:100%;
    overflow:auto;
    height:100%;
}
</style>